package com.sc.nft.controller;

import com.sc.nft.annotation.UserLogin;
import com.sc.nft.dao.RankingBoardConfDao;
import com.sc.nft.dao.RankingBoardDetailsNumberDao;
import com.sc.nft.dao.RankingBoardHistoryDetailsDao;
import com.sc.nft.entity.RankingBoardHistoryDetails;
import com.sc.nft.entity.dto.RankingListScreenDTO;
import com.sc.nft.entity.vo.RankingListScreenVO;
import com.sc.nft.service.RankingBoardConfService;
import com.sc.nft.service.RankingBoardDetailsNumberService;
import com.sc.nft.sup.Result;
import com.sc.nft.util.WeChatUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 排行榜配置表(RankingBoardConf)表控制层
 *
 * @author wwc
 * @since 2023-08-10 14:42:17
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("rankingBoardConf")
@Api(tags = "排行榜配置")
public class RankingBoardConfController {

    /**
     * 服务对象
     */
    private final RankingBoardConfService rankingBoardConfService;

    @ApiOperation(value = "排行榜筛选", notes = "排行榜筛选", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/rankingListScreen", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<RankingListScreenVO>> rankingListScreen(@Validated @RequestBody RankingListScreenDTO rankingListScreenDTO) {
        List<RankingListScreenVO> rankingListScreenVOS = rankingBoardConfService.rankingListScreen(rankingListScreenDTO);
        return Result.success(rankingListScreenVOS);
    }

    @ApiOperation(value = "历史榜单筛选", notes = "历史榜单筛选", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/historyRankingListScreen", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<RankingListScreenVO>> historyRankingListScreen(@Validated @RequestBody RankingListScreenDTO rankingListScreenDTO) {
        List<RankingListScreenVO> rankingListScreenVOS = rankingBoardConfService.historyRankingListScreen(rankingListScreenDTO);
        return Result.success(rankingListScreenVOS);
    }

    private final RankingBoardConfDao rankingBoardConfDao;

    private final RankingBoardDetailsNumberService rankingBoardDetailsNumberService;
    private final RankingBoardDetailsNumberDao rankingBoardDetailsNumberDao;
    private final RankingBoardHistoryDetailsDao rankingBoardHistoryDetailsDao;

    @ApiOperation(value = "1", notes = "1", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/rankingAward", produces = MediaType.APPLICATION_JSON_VALUE)
    public void rankingAward() {
        try {
            List<RankingBoardHistoryDetails> rankingBoardHistoryDetails = rankingBoardHistoryDetailsDao.leaderboardsToBeAwarded();
            rankingBoardHistoryDetails.forEach(rankingBoardHistoryDetail -> {
                rankingBoardDetailsNumberService.rankingAward(rankingBoardHistoryDetail);
            });
        } catch (Exception e) {
            WeChatUtil.sendWeChatMessage("发放奖励异常:" + e);
            log.error("发放奖励", e);
        }
    }
}
